package com.zy.pro.shop.web;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.shop.model.ShopUser;
import com.zy.pro.shop.service.ShopUserService;
import com.zy.pro.shop.web.form.ShopUserSearchForm;
import com.zy.pro.system.SystemCnt;
import com.zy.utils.MethodUtil;
import com.zy.utils.SessionUtils;
/**
 * 店家管理控制器
 *
 */
@Controller
@RequestMapping("/control/shop/shopUserController")
public class ShopUserController extends BaseController {

	private static final Logger logger = Logger.getLogger(ShopUserController.class);
	
	/**
	 * 店家管理服务类
	 */
	@Resource(name = "shopUserServiceImpl")
	private ShopUserService shopUserService;
	
	/**
	 * 店家登录
	 * @param shopUser
	 * @param map
	 * @param request
	 * @return
	 *//*
	@RequestMapping("/login")
	public void login(ShopUser shopUser, ModelMap map,HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		try {
			String message = "";
			if(shopUser != null && shopUser.getShopUserName() != null && !"".equals(shopUser.getShopUserName().trim()) && shopUser.getPassword() != null && !"".equals(shopUser.getPassword().trim())){
				shopUser.setPassword(MethodUtil.MD5(shopUser.getPassword()));
				ShopUser user = shopUserService.findShopUserNameAndPassword(shopUser);
				if(user != null && user.getShop() != null && user.getShop().getShopStatus() == 1){
					
					user.setShopUserOldIP(user.getShopUserNewIP());
					if(user.getShopUserNewLoginTime() != null){
						user.setShopUserLoginTime(user.getShopUserNewLoginTime());
					}
					user.setShopUserNewIP(SystemCnt.getIpAddr(request));
					user.setShopUserNewLoginTime(new Date());
					
					shopUserService.updateBySelective(user);
					
					//将店家信息放置到session当中
					SessionUtils.setShopUser(request, user);
					SessionUtils.setRoleType(request, user.getRoleType());
					response.getWriter().print(user.getShopUserId().toString()+"success");
				}else{
					if(user == null){
						message = "该卖家不存在或密码错误";
					}else if(user.getShop() == null || user.getShop().getShopStatus() == 3){
						message = "该卖家没有店铺信息";
					}else if(user.getShop().getShopStatus() == 2){
						message = "店铺已经被冻结,请联系管理员";
					}
					response.getWriter().print(message);
				}
			}else{
				if(shopUser != null){
					if(shopUser.getShopUserName() == null || "".equals(shopUser.getShopUserName().trim())){
						message = "用户名不能为空";
					}else{
						message = "密码不能为空";
					}
				}else{
					message = "用户名和密码不能为空";
				}
				response.getWriter().print(message);
			}
		} catch(Exception e){
			logger.error(e);
		}
	}
	
	*//**
	 * 展示卖家中心页面
	 * @return
	 *//*
	@RequestMapping("/displayShopCenter")
	public String displayShopCenter(ShopUserSearchForm form, ModelMap map,HttpServletRequest request){
		try {
			ShopUser shopUser = shopUserService.find(form.getShopUserId());
			map.addAttribute("shopUser", shopUser);
			map.addAttribute("shop", shopUser.getShop());
			return "/control/shop/shopCenter";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 展示卖家中心主页
	 * @return
	 *//*
	@RequestMapping("/displayTab")
	public String displayTab(ShopUserSearchForm form, ModelMap map,HttpServletRequest request){
		try {
			ShopUser shopUser = shopUserService.find(form.getShopUserId());
			map.addAttribute("shopUser", shopUser);
			map.addAttribute("shop", shopUser.getShop());
			return "/control/shop/tab";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 列出店家管理
	 *//*
	@RequestMapping("/list")
	public String list(ShopUserSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			
			
			//创建页面对象
			PageView<ShopUser> pageView = new PageView<ShopUser>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(shopUserService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 显示添加店家管理
	 *//*
	@RequestMapping("/displayAdd")
	public String displayAdd(ShopUser shopUser, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 添加店家管理
	 *//*
	@RequestMapping("/add")
	public String add(ShopUser shopUser, ModelMap map) {
		try {
			//TODO 设置shopUser  eg:主键信息
			
			shopUserService.save(shopUser);
			return list(new ShopUserSearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 显示编辑店家管理
	 *//*
	@RequestMapping("/displayEdit")
	public String displayEdit(ShopUserSearchForm form, ModelMap map) {
		try {
			//TODO 查询对象
			//eg:ShopUser shopUser = shopUserService.find(form.getId());
			//eg:map.addAttribute("entity", shopUser);
			
			
			//TODO 转发页面
			return "";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 编辑店家管理
	 *//*
	@RequestMapping("/edit")
	public String edit(ShopUser ShopUser, ModelMap map) {
		try {
			//TODO:处理店家管理
			
			
			shopUserService.updateBySelective(ShopUser);
			return list(new ShopUserSearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}

	*//**
	 * 删除单个店家管理
	 *//*
	@RequestMapping("/deleteshopUser")
	public String delete(ShopUserSearchForm form, ModelMap map) {
		try {
			//TODO  eg:shopUserService.delete(form.getId());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 批量删除店家管理
	 *//*
	@RequestMapping("/batchDelete")
	public String deletes(ShopUserSearchForm form, ModelMap map) {
		try {
//			TODO  eg:shopUserService.batchDelete(form.getIds());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}*/
}
